<!--  -->
<template>
    <div class="editForm">
        <el-dialog :title="dialogTitle+'用户'" :visible.sync="addDialogVisible" width="50%" :before-close="cancel">
            <el-form ref="addDialogFormRef" :model="addForm" :rules="addDialogFormRules" label-width="80px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username" :disabled="dialogTitle==='编辑'"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password" v-if="dialogTitle==='添加'">
                    <el-input v-model="addForm.password"></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="addForm.mobile"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="dialogTitle==='添加'?addUsers():editUsers()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: 'editForm',
        props: {
            addDialogVisible: {
                type: Boolean,
                default: () => {
                    return false
                }
            },
            dialogTitle: {
                type: String,
                default: () => {
                    return '添加'
                }
            },
            addForm: {
                type: Object,
                default: () => { }
            },
        },
        data() {
            //邮箱的校验规则
            var checkEmail = (rule, value, cb) => {
                const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
                if (regEmail.test(value)) {
                    return cb()
                }
                cb(new Error('请输入合法的邮箱!'))
            };
            //手机号的校验规则
            var checkPhone = (rule, value, cb) => {
                const regPhone = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
                if (regPhone.test(value)) {
                    return cb()
                }
                cb(new Error('请输入合法的手机号!'))
            };
            return {
                addDialogFormRules: {
                    username: [
                        { required: true, message: '请输入登录名称', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],

                    password: [
                        { required: true, message: '请输入登录密码', trigger: 'blur' },
                        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱', trigger: 'blur' },
                        { validator: checkEmail, trigger: 'blur' }

                    ],

                    mobile: [
                        { required: true, message: '请输入手机号', trigger: 'blur' },
                        { validator: checkPhone, trigger: 'blur' }
                    ]
                },
            }
        },
        methods: {
            addUsers() {
                // this.addForm = null;
                this.$refs.addDialogFormRef.validate(async valid => {
                    if (!valid) return
                    this.$emit('addUsers', this.addForm)
                })
            },
            editUsers() {
                this.$refs.addDialogFormRef.validate(async valid => {
                    if (!valid) return
                    this.$emit('editUsers', this.addForm)
                })
            },
            cancel() {
                this.$refs.addDialogFormRef.resetFields()
                this.$emit('cancel')
            }
        }
    }
</script>
<style lang="scss" scoped>

</style>